package library.database.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DatabaseProxy {
	private static Connection conn;
	/**
	 * Constructor, to get the connection.
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public DatabaseProxy() throws IOException, ClassNotFoundException, SQLException {
		String user = "root";
		String password = "123456";
		String url="jdbc:mysql://localhost:3306/user";
		String driverClass = "com.mysql.cj.jdbc.Driver";
		//2.加载驱动
		Class.forName(driverClass);
		//3.获取连接
		conn = DriverManager.getConnection(url,user,password);
		conn.setAutoCommit(false);
	}
	public static Connection getConnection() {
		return conn;
	}
	public static void closeResource(Connection conn,PreparedStatement ps) {
		try {
			if(ps != null)
				ps.close();}
			catch(SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null)
					conn.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}
}
